#include "ComponentLookAroundBehavior.h"
#include "Components/ComponentCreature.h"
#include "Components/ComponentBody.h"
#include "Components/ComponentCreatureSounds.h"
#include "Components/ComponentCreatureModel.h"

#include "Subsystems/SubsystemTerrain.h"
#include "GameRandom.hpp"
#include "Engine/MathUtils.hpp"
#include "Log.h"
#include "GameRegistry.hpp"
#include "GameSingleton.h"
#include "GameThreadPool.hpp"

#include <iostream>

namespace PocketSurvival
{

    // ************** 【状态机】 **************

    ComponentLookAroundBehavior::StateMachine::StateMachine(ComponentLookAroundBehavior *lookAroundBehavior) : m_lookAround(lookAroundBehavior), EasyMachine(MachineState::Inactive)
    {
        enter();
    }

    void ComponentLookAroundBehavior::StateMachine::enter()
    {
        switch (m_currentState)
        {
        case MachineState::Inactive:
            m_lookAround->m_importanceLevel = GameSingleton::gameRandom->getFloat(0.0f, 1.0f);
            break;
        case MachineState::LookAround:
            m_lookAround->m_lookAroundTime = GameSingleton::gameRandom->getFloat(8.0f, 15.0f);
            break;
        }
    }

    void ComponentLookAroundBehavior::StateMachine::update(float dt)
    {
        switch (m_currentState)
        {
        case MachineState::Inactive:
            MACHINE_DELEGATE()
            {
                if (m_lookAround->componentCreature->componentBody->standingOnValue != 0 && GameSingleton::gameRandom->getFloat(0.0f, 1.0f) < 0.05f * dt)
                {
                    m_lookAround->m_importanceLevel = GameSingleton::gameRandom->getFloat(1.0f, 5.0f);
                }
                if (m_lookAround->isActive())
                {
                    changeTo(MachineState::LookAround);
                }
            }
            break;
        case MachineState::LookAround:
            MACHINE_DELEGATE()
            {
                if (m_lookAround->isActive() == false)
                {
                    changeTo(MachineState::Inactive);
                }
                else if (m_lookAround->m_lookAroundTime <= 0.0f)
                {
                    m_lookAround->m_importanceLevel = 0.0f;
                }
                else if (GameSingleton::gameRandom->getFloat(0.0f, 1.0f) < 0.1f * dt)
                {
                    // m_lookAround->componentCreature->componentCreatureSounds->playIdleSound(false);
                }
                m_lookAround->componentCreature->componentCreatureModel->lookRandomOrder = true;
                m_lookAround->m_lookAroundTime -= dt;
            }
            break;
        }
    }

    void ComponentLookAroundBehavior::StateMachine::leave() {}

    // **************************************



    ComponentLookAroundBehavior::~ComponentLookAroundBehavior()
    {
        delete m_stateMachine;
    }

    float ComponentLookAroundBehavior::getImportanceLevel()
    {
        return m_importanceLevel;
    }

    void ComponentLookAroundBehavior::update(float dt)
    {
        m_stateMachine->update(dt);
    }

    static const std::string componentName = "LookAroundBehavior";
    const std::string &ComponentLookAroundBehavior::getName() const
    {
        return componentName;
    }
    const std::string &ComponentLookAroundBehavior::GetName()
    {
        return componentName;
    }
    void ComponentLookAroundBehavior::load(const nlohmann::json &jsonData)
    {
        componentCreature = GameSingleton::gameRegistry->get<ComponentCreature *>(entity);

        m_stateMachine = new StateMachine(this);
    }

}
